<?php
include('includes/connexion.php');
$adminmanager = new AdminManager($connexion);
$elevemanager = new EleveManager($connexion);
session_start();
include('includes/secu.php');
	
$exercicemanager = new ExerciceManager($connexion);// on instancie un exercice avec la connexion en parametre
$testmanager = new TestManager($connexion); //  on instancie un test pour récupérer le nom du test
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <title>Tables Advanced - Target Admin</title>

  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300,700">
  <link rel="stylesheet" href="./css/font-awesome.min.css">
  <link rel="stylesheet" href="./js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.min.css">
  <link rel="stylesheet" href="./css/bootstrap.min.css">

  <!-- Plugin CSS -->
  <link rel="stylesheet" href="./js/plugins/icheck/skins/minimal/blue.css">
  <link rel="stylesheet" href="js/plugins/select2/select2.css">
  <link rel="stylesheet" href="js/plugins/datepicker/datepicker.css">
  <link rel="stylesheet" href="js/plugins/simplecolorpicker/jquery.simplecolorpicker.css">
  <link rel="stylesheet" href="js/plugins/timepicker/bootstrap-timepicker.css">
  <link rel="stylesheet" href="js/plugins/fileupload/bootstrap-fileupload.css">
  
  <!-- App CSS -->
  <link rel="stylesheet" href="./css/target-admin.css">
  <link rel="stylesheet" href="./css/custom.css">


  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  <![endif]-->
</head>
<?php 
	$tabExercice = $exercicemanager->getList(); // on récupere la liste des exercices dans le tableau
	
	// on update un champ
	if(!empty($_POST['IntituleUpdate']) && !empty($_POST['DureeUpdate'])){
		$mex = $exercicemanager->get($_POST["idExUpdate"]);
		$mex->setIntitule($_POST['IntituleUpdate']);
		$mex->setDuree_seconde($_POST['DureeUpdate']);
		$exercicemanager->update($mex);
		header('Location:listexercice.php'); // on redirige	
	}	
	
	// si les champs del et id existent on supprime le test.
	if(!empty($_GET['del']) && !empty($_GET['id'])){
		$exercicemanager->delete($exercicemanager->get($_GET["id"])); // on supprime le test
		header('Location:listexercice.php'); // on redirige	
	}  	

    // si les champs sont remplis on ajoute à la bdd         	
	if(!empty($_POST['nomExercice']) && !empty($_POST['duree'])){
		$monExercice = new Exercice(); // on créé un nouvel exercice
		$monExercice->setIntitule($_POST['nomExercice']); // on ajoute l'intitulé à l'exercice
		$monExercice->setDuree_seconde($_POST['duree']); // on ajoute la duree à l'exercice
		$monExercice->setType($_POST['typeExo']); // on ajoute le type au test
		$monExercice->setIdRefExemple(0);
		$exercicemanager->add($monExercice); // on ajoute le test à la bdd
		
		header('Location:listexercice.php'); // on redirige			
	}
				
?>
<body>

<?php include('includes/navbar.php'); ?>

<?php include('includes/mainbar.php'); ?>

<div class="container">

  <div class="content">

    <div class="content-container">

      

      <div class="content-header">
        <h2 class="content-header-title">Exercices</h2>
        <a href="gestion.php">Tests</a> / Exercices
      </div> <!-- /.content-header --> 

      <div class="row">
      

        <div class="col-md-12">

          <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-table"></i>
                Liste des exercices <?php if(isset($idTest)){ echo "du test ".$testmanager->get($idTest)->intitule(); }?>
              </h3>

            </div> <!-- /.portlet-header -->

            <div class="portlet-content">           

              <div class="table-responsive">


	  <table class="table table-striped table-bordered table-hover table-highlight table-checkable" 
                data-provide="datatable" 
                data-display-rows="10"
                data-info="true"
                data-search="true"
                data-length-change="true"
                data-paginate="false">
        <thead>
	    <tr>
          <center>
            <th data-filterable="false" data-sortable="true" data-direction="desc">OPTION</th>
            <th data-filterable="false" data-sortable="true" data-direction="desc">EXERCICE</th>
            <th data-filterable="false" data-sortable="true" data-direction="desc">DUREE (secondes)</th>
            <th data-filterable="false" data-sortable="true" data-direction="desc">TYPE</th>
          </center>
        </tr>
        </thead>
        <tbody>
        <?php 
		foreach($tabExercice as $asso){
			if(isset($idTest))
				$exercice = $exercicemanager->get($asso['idRefExercice']);
			else
				$exercice = $asso;
			
			
			$montype = $exercice->type(); // on regarde le type d'exo
			switch ($montype){
				case "R" :
					$typeAff = "Relations"; // sert à l'affichage
					break;
				case "A" :
					$typeAff = "Analogie";
					break;
				case "BB" :
					$typeAff = "Briques et Blocs";
					break;
				case "D" :
					$typeAff = "Dominos";
					break;
				case "IP" :
					$typeAff = "Intéret Professionnel";
					break;
				case "Q" :
					$typeAff = "Questionnaire";
					break;
				case "SN" :
					$typeAff = "Série Numérique";
					break;
				case "TT" :
					$typeAff = "Texte à Trous";
					break;
				case "G" :
					$typeAff = "Générique";
					break;
			}			
			

			echo "<tr id='exercice".$exercice->id()."'>";
			echo "<td align='center'>";
			
					?><a data-toggle="modal" href="#ModifExercice" class="fa fa-gear" id="exercice<?php echo $exercice->id(); ?>"></a> | <a href ='?del=1&id=<?php echo $exercice->id(); ?>' style='color:red;' onclick="return(confirm('Etes-vous sûr de vouloir supprimer cet exercice ?'));"><span class="fa fa-trash-o"></span></a><?php
					if($exercicemanager->getExemple($exercice->id()) == false){
						$nbExemple = 0;
					}else {$nbExemple = 1;}
			echo " | <a href ='question.php?idExercice=".$exercice->id()."'>Gestion des questions (".$exercicemanager->getNbQuestion($exercice->id()).")</a>";
			if($exercice->type() != "Q" && $exercice->type() != "IP"){
			echo " |
				  <a href ='exemples.php?idExercice=".$exercice->id()."'>Gestion des exemples (".$nbExemple.")</a>
				  </td>";
			}
			echo "<td>".$exercice->intitule()."</td>";
			echo "<td>".$exercice->duree_seconde()."</td>";
			echo "<td>".$typeAff."</td>
				  </tr>";
		
		}
		?>
        </tbody>
	    <tr>
          
          	<form action="listexercice.php" method="post">
            	  <td align="center"><input type="submit" class="btn btn-success" value="Ajouter un Exercice"/></td>               
                  <td><input type="text" name="nomExercice" /></td>
                  <td><input type="text" name="duree"/></td>
                  <td><select name="typeExo">
                  		  <option value="G">Générique</option>
                          <option value="R">Relations</option>
                          <option value="A">Analogies</option>
                          <option value="TT">Texte à trous</option>
                          <option value="D">Dominos</option>
                          <option value="SN">Séries numériques</option>
                          <option value="BB">Briques et blocs</option>
                          <option value="IP">Intérêts professionnels</option>
                          <option value="Q">Questionnaire</option>                          
					  </select>
                  </td>
            </form>
          
        </tr>
      </table>
              </div> <!-- /.table-responsive -->

            </div> <!-- /.portlet-content -->

          </div> <!-- /.portlet -->

        </div> <!-- /.col -->

      </div> <!-- /.row -->

    </div> <!-- /.content-container -->
      
  </div> <!-- /.content -->

</div> <!-- /.container -->


<?php include('includes/footer.php'); ?>

  <script src="./js/libs/jquery-1.10.1.min.js"></script>
  <script src="./js/libs/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="./js/libs/bootstrap.min.js"></script>

  <!--[if lt IE 9]>
  <script src="./js/libs/excanvas.compiled.js"></script>
  <![endif]-->
  
  <!-- Plugin JS -->
  <script src="./js/plugins/datatables/jquery.dataTables.min.js"></script>
  <script src="./js/plugins/datatables/DT_bootstrap.js"></script>
  <script src="./js/plugins/tableCheckable/jquery.tableCheckable.js"></script>
  <script src="./js/plugins/icheck/jquery.icheck.min.js"></script>

  <!-- App JS -->
  <script src="./js/target-admin.js"></script>
  
<div id="ModifExercice" class="modal modal-styled fade">
  <div class="modal-dialog">
    <div class="modal-content">
    <form action="listexercice.php" id="mformexercice" method="post" class="form parsley-form">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3 class="modal-title">Modification de l'exercice</h3>
      </div>
      <div class="modal-body">
      	<div class="row">
              <div class="col-md-6">
              <label for="IntituleUpdate">Intitulé</label><input type="text" name="IntituleUpdate" />
              </div>
              <div class="col-md-6">
              <label for="DureeUpdate">Durée</label><input type="text" name="DureeUpdate" />
              </div>
          </div>
      </div>
      <div class="modal-footer">
      	<input type="text" name="idExUpdate" hidden/>
        <button type="button" class="btn btn-tertiary" data-dismiss="modal">Fermer</button>
        <input type="submit" class="btn btn-primary" name="mformexercice" value="Sauvegarder"/>
      </div>
      </form>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script type="text/javascript">
$(document).ready(function() {
	$("a[id^='exercice']").click(function() {
		
		var id = $(this).attr("id").split("exercice");

		var test3 = '#exercice'+id[1]+' td+td';
		var test4 = '#exercice'+id[1]+' td+td+td';
		
		$("#mformexercice input[name=idExUpdate]").val(id[1]);		
				
		$("#mformexercice input[name=IntituleUpdate]").val($(test3).html());
		
		$("#mformexercice input[name=DureeUpdate]").val($(test4).html());
	});
});
</script>
</body>
</html>